package net.k3rnel.abugadro.util;

import net.k3rnel.abugadro.ProjectsDB;
import android.content.ContentResolver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class SyncService{

	private volatile SyncThread mSyncThread;
	boolean mSync = false;

	public synchronized void startThread(Handler h, ContentResolver cr, Uri uri, int type){
		if(mSyncThread == null){
			mSyncThread = new SyncThread(h,cr, uri, type);
			mSyncThread.start();
		}
	}
	public synchronized void stopThread(){
		if(mSyncThread != null){
			Thread moribund = mSyncThread;
			mSyncThread = null;
			moribund.interrupt();
		}
	}	

	public boolean isSync() {
		return mSync;
	}

	/** Nested class that performs progress calculations (counting) */
	public class SyncThread extends Thread {
		Handler mHandler;
		ContentResolver mCr;
		int type = 0;
		Uri uri;
		SyncThread(Handler h, ContentResolver cr, Uri uri, int type) {
			mCr = cr;
			mHandler = h;
			this.uri = uri;
			this.type = type;
		}



		public void run() {  
			Message msg = mHandler.obtainMessage();
			Bundle b = new Bundle();
			b.putInt("status", 1);
			msg.setData(b);
			mHandler.sendMessage(msg);
			try{
				mSync = true;
				final int gcodeId = Integer.parseInt(uri.getPathSegments().get(1));
				final int projId = ProjectsDB.getInstance().getProjectIdbyGoogleId(mCr,gcodeId);
				final String projName = ProjectsDB.getInstance().getProjectName(mCr,projId);
				switch(type){
				case 0: // Latest Issues
					new SaxFeedParser(mCr,"http://code.google.com/feeds/issues/p/"+projName+"/issues/full",gcodeId,0,0).parse();
					break;
				case 1: // All Issues
					new SaxFeedParser(mCr,"http://code.google.com/feeds/issues/p/"+projName+"/issues/full?max-results=1400",gcodeId,0,0).parse();
					break;
				case 2: // Open Issues
					new SaxFeedParser(mCr,"http://code.google.com/feeds/issues/p/"+projName+"/issues/full?max-results=1400&can=open",gcodeId,0,0).parse();
					break;
				case 3: // My Issues
					new SaxFeedParser(mCr,"http://code.google.com/feeds/issues/p/"+projName+"/issues/full?max-results=1400&can=owned",gcodeId,0,0).parse();
					break;
				case 4: // Starred Issues
					new SaxFeedParser(mCr,"http://code.google.com/feeds/issues/p/"+projName+"/issues/full?max-results=1400&can=starred",gcodeId,0,0).parse();
					break;
				case 5: // Reported Issues
					new SaxFeedParser(mCr,"http://code.google.com/feeds/issues/p/"+projName+"/issues/full?max-results=1400&can=reported",gcodeId,0,0).parse();
					break;
				case 6: //Issue Comments
					int issueId = Integer.parseInt(uri.getPathSegments().get(3));
					new SaxFeedParser(mCr,"http://code.google.com/feeds/issues/p/"+projName+"/issues/"+issueId+"/comments/full",gcodeId,issueId,1).parse();
					break;
				case 7: //GCode SVN
					new SaxFeedParser(mCr,"http://code.google.com/feeds/p/"+projName+"/svnchanges/basic",gcodeId,0,2).parse();
					break;
				case 8: //GCode HG
					new SaxFeedParser(mCr,"http://code.google.com/feeds/p/"+projName+"/hgchanges/basic",gcodeId,0,3).parse();
					break;
				}
			}catch(Exception e){
				Log.e("Abugadro","SyncService",e);
				msg = mHandler.obtainMessage();
				b = new Bundle();
				b.putInt("status", 0);
				msg.setData(b);
				mHandler.sendMessage(msg);
			}
			msg = mHandler.obtainMessage();
			b = new Bundle();
			mSync = false;
			b.putInt("status", 1);
			msg.setData(b);
			mHandler.sendMessage(msg);
			stopThread();
		}
	}
}